import Vue from 'vue'
import Vuex from 'vuex'
import Http from '../common/js/http.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		rootSize: '100px',
		first: 0,
		second: -1,
		third: -1,
		currRoute: '',
		siteInfo: null,
		memberInfo: null,
		pendOrderNum: 0,
		storeInfo: null,
		userInfo: null,
		addon: [],
		menu: [],
		defaultImg: {
			goods: '',
			head: '',
			store: '',
			article: ''
		},
	},
	mutations: {
		setRootSize(state, value) {
			state.rootSize = value;
		},
		setMemuIndex(state, value) {
			state[value.level] = value.index;
		},
		setCurrRoute(state, value) {
			state.currRoute = value;
		},
		setSiteInfo(state, value) {
			state.siteInfo = value;
		},
		setMemberInfo(state, value) {
			state.memberInfo = value;
		},
		setPendOrderNum(state, value) {
			state.pendOrderNum = value;
		},
		setStoreInfo(state, value) {
			state.storeInfo = value;
			uni.setStorageSync('store_info', value);
		},
		setAddon(state, value) {
			state.addon = value;
		},
		setUserInfo(state, value) {
			state.userInfo = value;
		},
		setMenu(state, value) {
			state.menu = value;
		},
		setDefaultImg(state, value) {
			uni.setStorageSync('default_img', value)
			state.defaultImg = value;
		},
	},
	actions: {
		getSiteInfo() {
			Http.sendRequest({
				url: '/siteapi/site/info',
				success: res => {
					if (res.code == 0) {
						this.commit('setSiteInfo', res.data)
					}
				}
			})
		},
		getStoreInfo(state, params) {
			Http.sendRequest({
				url: '/cashier/storeapi/store/info',
				success: res => {
					if (res.code == 0) {

						if (res.data && res.data.status == 0) {
							uni.navigateTo({
								url: '/pages/store/close'
							});
							return;
						}

						this.commit('setStoreInfo', res.data);
						if (params && params.callback) params.callback();
					}
				}
			})
		},
		getAddonIsExit() {
			Http.sendRequest({
				url: '/storeapi/addon/addonisexit',
				success: res => {
					if (res.code == 0) {
						this.commit('setAddon', res.data)
					}
				}
			})
		},
		getUserInfo() {
			Http.sendRequest({
				url: '/cashier/storeapi/user/userinfo',
				success: res => {
					if (res.code == 0) {
						this.commit('setUserInfo', res.data)
					}
				}
			})
		},
		getUserGroup() {
			Http.sendRequest({
				url: '/cashier/storeapi/user/usergroupauth',
				success: res => {
					if (res.code == 0) {
						if (res.code == 0 && res.data) {
							let menu = require('@/common/menu/store.js').default ?? [];
							let addon = this.state.addon;
							const checkAuth = function(menu, auth) {
								let newMenu = [];
								menu.map(item => {
									if (item.children) {
										item.children = checkAuth(item.children, auth);
									}
									if (item.addon && addon.indexOf(item.addon) == -1)
										return;
									if (item.name && !res.data.is_admin && auth
										.length && auth.indexOf(item.name) == -1) {
										newMenu.push({});
										return;
									}

									newMenu.push(item);
								})
								return newMenu;
							}
							menu = checkAuth(JSON.parse(JSON.stringify(menu)), res.data.menu_array ?
								res.data.menu_array.split(',') : []);
							this.commit('setMenu', menu);
						}
					}
				}
			})
		},
		getDefautlImg() {
			Http.sendRequest({
				url: '/api/config/defaultimg',
				success: res => {
					if (res.code == 0) {
						this.commit('setDefaultImg', res.data)
					}
				}
			})
		},
	}
})
export default store